<?php
// PXFInstance::breakPoint($current_categories);

class ControllerAdmin extends PXFControllerAdmin
{
	static $tablinks = null;

	public function __construct()
	{ 
		self::$tablinks = array(
			array('controller' => 'ajax_posts', 'action' => 'index',  'title' => 'Listado de Articulos'),
			array('controller' => 'ajax_posts', 'action' => 'create', 'title' => 'Nuevo Articulo'),
		);
		parent::__construct();
		$this->tpl->assign('tablinks',self::$tablinks);
		$this->tpl->assign('ITEM_TYPE','posts');
	}

	public function index()
	{
		PXFConfigAdmin::$site['page_title'] = 'Productos';
		$this->tpl->assign('site',PXFConfigAdmin::$site);
		$this->tpl->display('ajax_items.list.tpl');
	}
	
	public function post_save()
	{
    /* Required params */
	  $id = PXFRequest::reqGetOr404('id'); 
	
	  /* Models */
	  $mod_posts = PXFModel::getModelNLS('item');
	  $post = $mod_posts->find($id);
	  PXFRequest::notNullOr404($post); 

	  /* Validation. */
    $validator = PXFValidator::getNew();
	  $price = new ValidatorVoid('price',false);
	  $visibility = new ValidatorVoid('visibility',true);
	  $validator->addItem($price);
	  $validator->addItem($visibility);
	  $cmmnt = new ValidatorVoid('comments',true);
	  $mlvs = array(); /* multilang validators */
	
	  foreach(PXFConfig::$languages as $key => $value)
    {
			$title = sprintf('title_%s',$key);
			$mlvs[$title] = new ValidatorVoid($title,true);
			$validator->addItem($mlvs[$title]);
				
			$intro = sprintf('intro_%s',$key);
			$mlvs[$intro] = new ValidatorVoid($intro,false);
			$validator->addItem($mlvs[$intro]);
						
			$cont = sprintf('content_%s',$key);
			$mlvs[$cont] = new ValidatorVoid($cont,false);
			$validator->addItem($mlvs[$cont]);
    }
		
		$errors = $validator->validate();
		$post->price = $price->getValue();
		$post->visibility = $visibility->getValue();
		$post->edited_at = date('Y-m-d h:i:s');
		$post->edited_by = PXFInstance::$user->id;
		$post->resource_id = md5($post->id);
	  $post->comments = $cmmnt->getValue();
		
		foreach($mlvs as $key => $value)
		{
			$post->$key = $value->getValue();
		}
	
	  if(!$errors)
		{
			$post->save();
			PXFRequest::redirect('index.php?c=ajax_posts');
		}
		json_encode(array('errors'=> $errors));
  }

	public function item()
	{
		$id = PXFRequest::reqGetOr404('id'); 
		$mod_post = PXFModel::getModelNLS('item');
		$post = $mod_post->find($id);

		PXFConfigAdmin::$site['page_title'] = 'Editar Articulo';
		$this->tpl->assign('site',PXFConfigAdmin::$site);
		$this->tpl->assign('post',$post);
		$this->tpl->display('ajax_posts.interface.tpl');
	}

	public function create()
	{
	  $mod_posts = PXFModel::getModel('item');
	  $post = $mod_posts->getNew();
		$post->visibility = 3;
		$date = date('Y-m-d h:i:s');
		$post->title = 'Nuevo Post';
		$post->created_at = $date;
		$post->created_by = PXFInstance::$user->id;
		$post->edited_at = $date;
		$post->edited_by = PXFInstance::$user->id;
		$post->typed = 'posts';
		$post->resource_id = md5(time());
		$post->save();
		PXFRequest::redirect(sprintf('index.php?c=ajax_posts&a=item&id=%s',$post->id));
	}

	public function post_delete()
	{
	  $id = PXFRequest::reqGetOr404('id'); 
	  $mod_posts = PXFModel::getModel('item');
		$post = $mod_posts->find($id);

		foreach($post->category() as $cat)
		{
			$post->category->remove($cat);
		}

		foreach($post->images() as $img)
		{
			$img->delete();
		}
		$post->delete();
		PXFRequest::redirect('index.php?c=ajax_posts');
	}
}
?>
